package com.example.big_orange2.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.big_orange2.authorization.annotation.AuthIgnore;
import com.example.big_orange2.common.Result;
import com.example.big_orange2.entity.*;

import com.example.big_orange2.mapper.UserMapper;
import com.example.big_orange2.mapper.rechargeorderMapper;
import com.example.big_orange2.service.RechargeOrderService;
import com.example.big_orange2.service.StaffService;
import javafx.util.Pair;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/rechargeorder")
public class rechargeorderController {

    @Resource
    rechargeorderMapper rechargeorderMapper ;
    @Resource
    UserMapper userMapper;
    @Resource
    RechargeOrderService rechargeOrderService;
    //增
    //向数据库写入（insert）数据的接口
    //充值并更新余额
    @AuthIgnore
    @PostMapping("/add")
    public Result<?> save(@RequestBody rechargeorder recharge){//?是泛型，表示任意类型
        rechargeorderMapper.insert(recharge);
//        rechargeorderMapper.chongzhi(recharge.getUserid(),recharge.getAmount());
        rechargeOrderService.addmoney(recharge.getUserid(),recharge.getAmount());
        return Result.success();//返回操作情况
    }

    //向数据库删除（delete）数据的接口
    @DeleteMapping("/delete/{rechargeid}")//占位
    public Result<?> delete(@PathVariable String rechargeid){//?是泛型，表示任意类型
        //删除的操作
        rechargeorderMapper.deleteById(rechargeid);
        return Result.success();//返回操作情况

    }
    //修改功能
    @PutMapping("/update")
    //向数据库更新（update）数据的接口
    public Result<?> update(@RequestBody rechargeorder rechargeorder){//?是泛型，表示任意类型
        rechargeorderMapper.updateById(rechargeorder);
        return Result.success();
    }

    //全部查询
    @GetMapping("/getdata")
    //根据员工号，挑选他负责的客户信息
    public Result<?> finduser(){
        LambdaQueryWrapper<rechargeorder> wrapper = Wrappers.<rechargeorder>lambdaQuery();
        List<rechargeorder> Loanhandle = rechargeorderMapper.selectList(wrapper);//
        return  Result.success(Loanhandle);//返回结果

    }

    //依据序号查询
    @GetMapping("/getdataseq/{rechargeid}")
    public Result<?> findbyseq(@PathVariable String rechargeid){
        LambdaQueryWrapper<rechargeorder> wrapper =Wrappers.<rechargeorder>lambdaQuery();
        wrapper.eq(rechargeorder::getRechargeid,rechargeid);//多条件查询
        List<rechargeorder> Loanhandle = rechargeorderMapper.selectList(wrapper);//
        return  Result.success(Loanhandle);//返回结果

    }
    //依据用户id查询
    @GetMapping("/getdatauser/{userid}")
    public Result<?> findbyuser(@PathVariable String userid){
        LambdaQueryWrapper<rechargeorder> wrapper =Wrappers.<rechargeorder>lambdaQuery();
        wrapper.eq(rechargeorder::getUserid,userid);//多条件查询
        List<rechargeorder> Loanhandle = rechargeorderMapper.selectList(wrapper);//
        return  Result.success(Loanhandle);//返回结果

    }
    @GetMapping("/getdatass")
    public Result<?> findbysum(){
        Result<?> result=new Result<>();
        result=rechargeOrderService.findbysum();
        return  result;
    }

    @PostMapping("/paging/{page}/{limit}")
    //分页查询(select),查询数据接口
    public Result<?> findPage(@PathVariable Integer page, @PathVariable Integer limit,
                              @RequestBody(required = false)  String s)
    {
        Result<?> result=new Result<>();
        result=rechargeOrderService.findPage(page,limit,s);
        return  result;

    }
}
